import regeneratorRuntime from '../../plugins/regenerator-runtime.js'
import { addVisitorFrequency } from '../../utils/encapsulation.js'//数据监控
import { Request_API_CN, Request_API_COM } from '../../utils/index.js'//封装请求
import login from '../../api/login.js';
import { foolData } from '../../data/foolData.js';
const app = getApp()
const config = { tablename: surface }
import { prefix, surface } from '../../api/config.js';
Page({
  data: {
    musicUrl: 'https://bycx.yyjpai.cn/program/20181116/bg_audio.mp3',
    userInfo: false, //判断授权
    explainShow: false, //说明弹窗
    payShow: false, //支付弹窗
    enoughShow: false, //余额不足弹窗
    shopList:[], //商品列表
    balance: 0 , //余额
    moneyList: [], //充值弹窗金额
    music: true,  //默认播放状态
    staticData: {}, //静态数据
    broadcast: [
      { name: '佛系小可', shop: '阿玛尼红管口红' },
      { name: '月牙小淑女', shop: 'M·A·C 魅可口红' },
      { name: '长安有故里', shop: 'YSL圣罗兰黑管口红' },
      { name: '老子叫甜甜', shop: 'DIOR迪奥釉质唇膏口红' },
      { name: '丝丝喵', shop: 'CHANEL香奈儿炫亮口红' },
      { name: '巡山小妖精', shop: '阿玛尼黑管口红' },
      { name: '小饭团', shop: '纪梵希哑光小羊皮口红' },
      { name: '李哈哈', shop: '汤姆福特烈焰幻魅口红' },
      { name: '不晚', shop: 'M·A·C 魅可口红' },
      { name: '辣条小仙女', shop: 'YSL圣罗兰方管口红' },
      { name: '半秋', shop: 'YSL圣罗兰莹亮唇魅口红' },
      { name: '看你笑', shop: 'DIOR迪奥烈艳蓝金口红' },
      { name: '中意你', shop: 'DIOR迪奥魅惑染口红' },
      { name: '怪', shop: 'CHANEL香奈儿炫亮口红' },
      { name: '有刺的猬', shop: '阿玛尼细管口红' },
      { name: '几番', shop: '纪梵希哑光小羊皮口红' },
      { name: '青冘', shop: '纪梵希哑光小羊皮口红' },
      { name: '秋风别来', shop: '汤姆福特烈焰幻魅口红' },
      { name: '酷性泛滥', shop: '阿玛尼红管口红' },
      { name: '9527我是', shop: 'CHANEL香奈儿炫亮口红' },
      { name: '不敢恋人', shop: '阿玛尼红管口红' }
    ],
    isHide: false,
    jumpShow: false, //跳过视频
    videoShow: true, //视频显示影藏
    cheatData:[]//骗审数据
  },
  onLoad: function() {
    wx.hideTabBar({animation: true});
    this.videoContext = wx.createVideoContext('myVideo');
    this.getChaet();
    this.getShopList();
    this.getStatic();
    this.lipstickShare();//分享图片
    this.getBanner();
  },
  onShow: function() {
    if (this.data.music && this.innerAudioContext) {
      this.innerAudioContext.play();
    }
    this.authorize();
    if (this.data.isHide) {
      this.videoClose();
    }
  },
  formSubmit: (e) => { //模板消息
    let openid = app.globalData.openid;
    let formId = e.detail.formId;
    let data = {
      openid: openid,
      fid: formId,
      tablename: surface
    }
    // console.log(data)
    Request_API_CN('/currency/getfid', 'POST', data).then(res => {
      // console.log(res)
    })
  },
  getChaet:function() { //小程序标题
    Request_API_CN(prefix+'/edition', 'POST', {}).then(res => {
      // console.log(res)
      wx.setNavigationBarTitle({//小程序名字
        title: res.data.data.appname
      })
      console.log()
      this.setData({
        twoB: res.data.data.id == 2 ? true : false,//骗审数据
        cheatData: foolData
      })
      // console.log(foolData)
      // console.log(this.data.twoB)
    })
  },

  videoWait: function () { //视频开始缓冲
    // console.log('视频开始缓冲')
  },
  videoProgress: function () { //加载进度变化
    // console.log('加载进度变化')
    this.setData({ jumpShow: true })
  },
  getBanner: function () { //轮播图
    Request_API_CN(prefix +'/getBanner', 'POST', {}).then(res => {
      console.log(res.data.data)
      this.setData({ banner: res.data.data })
    })
  },
  videoPlay: function () { //视频开始播放
    // console.log('视频开始播放')
    this.setData({ videoShow: true })
    setTimeout(res => (this.setData({ jumpShow: true })), 1500)
  },
  videoStop: function () { //关闭视频
    // console.log('关闭视频')
    wx.showTabBar({ animation: true });
    this.videoClose();
    this.musicAudio();
  },
  videoEnd: function () { //视频播放结束
    // console.log('视频播放结束')
    wx.showTabBar({ animation: true });
    this.videoClose();
    this.musicAudio();
  },
  videoClose: function () {
    this.videoContext.stop();
    this.setData({ videoShow: false, jumpShow: false, isHide: true })
  },
  enoughTap: function () { this.setData({ enoughShow: false, payShow: true }) }, //余额不足，马上充值
  payTap: function(e) { //开始闯关购，判断余额
    wx.showLoading({
      title: '加载中...',
    })
    let shopId = e.currentTarget.dataset.shopid;
    Request_API_CN(prefix +'/getBalance', 'POST', { openid: app.globalData.openid }).then(res => {
      // console.log(res.data.data.balance)
      this.setData({
        balance: res.data.data.balance,
        moneyList: res.data.data.rechargeNum
      })
      let price = e.currentTarget.dataset.price;
      if (price > this.data.balance) { //余额不足，显示弹窗
        this.setData({ enoughShow: true })
        wx.hideLoading();
      } else {
        this.innerAudioContext.pause();
        Request_API_CN(prefix +'/startGame', 'POST', { openid: app.globalData.openid, lipstick_id: shopId }).then(res => {
          wx.navigateTo({
            url: '/pages/game/game?game=formal&orderId=' + res.data.data.lrid
          })
          wx.hideLoading();
        })
      }
    })
  },
  payMoneyTap:function(e) { //支付
    let price = e.currentTarget.dataset.money;
    let openid = app.globalData.openid;
    Request_API_CN(prefix +'/unifiedorder', 'POST', { openid: openid, price: price}).then(res=>{
      // console.log(res.data.data)
      this.wxPay(res.data.data);
    })
  },
  wxPay: function (data) { //调用微信支付
    wx.requestPayment({
      timeStamp: data.timeStamp+'',
      nonceStr: data.nonceStr,
      package: data.package,
      signType: 'MD5',
      paySign: data.paySign,
      success: res => { 
        this.closePopup();
      },
      fail: res=>{
        // console.log(res)
        this.closePopup();
      }
    })
  },
  musicAudio:function() { //播放音乐
    this.innerAudioContext = wx.createInnerAudioContext()
    this.innerAudioContext.autoplay = true;
    this.innerAudioContext.loop = true;
    this.innerAudioContext.src = this.data.musicUrl
    this.innerAudioContext.onPlay(() => {
      // console.log('开始播放')
    })
    this.innerAudioContext.onError((res) => {
      // console.log(res.errMsg)
      // console.log(res.errCode)
    })
  },
  musicControl: function() { //音乐播放暂停
    this.setData({ music: !this.data.music })
    if(this.data.music) {
      this.innerAudioContext.play();
    } else {
      this.innerAudioContext.pause();
    }
  },
  explainTap: function() { //闯关购说明
    this.setData({ explainShow: true })
  },
  closePopup: function() { //关闭弹窗
    this.setData({ explainShow: false, payShow: false, enoughShow: false })
  },
  authorize: function () {//判断用户是否授权

    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          // console.log("no auth")
          this.setData({ userInfo: false })
        } else {
          this.setData({ userInfo: true })
          // console.log("has auth")
          wx.getUserInfo({
            lang: "zh_CN",
            success: (res) => {
              app.globalData.userInfo = res.userInfo;
              this.userInfo(res.userInfo)
            }
          })
        }
      }
    })
  },
  userInfo: function (e) {//提交用户信息
    let obj = {}
    let tablename = surface;
    obj.openid = app.globalData.openid;
    obj.nickname = e.nickName;
    obj.gender = e.gender;
    obj.city = e.city;
    obj.province = e.province;
    obj.avatar = e.avatarUrl;
    obj.aid = app.globalData.AID ? app.globalData.AID : '';
    obj.tablename = tablename;
    console.log(obj)
    obj.inviterOpenid = app.globalData.inviterOpenid ? app.globalData.inviterOpenid : '' 
    Request_API_CN('/currency/firstLogin', 'POST', { openid: app.globalData.openid, tablename: tablename }).then((res) => {
      if (res.data.data.isFirstLogin == 1) {
        Request_API_CN('/currency/userInfo', 'POST', obj).then(res=>{
          this.getMoney();
        })
      } else {
        this.getMoney();
      }
    })
  },
  getShopList:function() { //商品列表
    Request_API_CN(prefix +'/lipstickList','POST',{}).then(res=>{
      this.setData({ shopList: res.data.data })
    })
  },
  getMoney: function () { //余额，充值弹窗金额
 
  },
  getStatic: function () { //静态资源
    Request_API_CN(prefix +'/characters', 'POST', {}).then(res => {
      console.log(res.data)
      this.setData({ staticData: res.data.data })
    })
  },
  playgame:function() {
    this.innerAudioContext.pause();
    wx.navigateTo({
      url: '/pages/game/game?game=demo&orderId='
    })
  },
  lipstickShare: function()  {//分享数据
    Request_API_CN(prefix + '/lipstickShare', 'POST', { openid: app.globalData.openid, tablename: surface })
      .then((res) => {
        this.setData({
          imgurl: res.data.data.imgurl,
          title: res.data.data.title
        })
      })
  },
  onShareAppMessage: function(e) {
    return {
      path: '/pages/index/index?inviterOpenid=' + app.globalData.openid,
      title: this.data.title,
      imageUrl: this.data.imgurl
    }
  }
})